package com.haier.bims.frozenbox.controller;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haier.bims.frozenbox.bean.Frozenbox;
import com.haier.bims.frozenbox.bean.FrozenboxList;
import com.haier.bims.frozenbox.service.IFrozenboxService;
import com.haier.bims.samples.bean.Samples;
import com.haier.bims.samples.bean.SamplesList;
import com.haier.common.AbstractController;

@Controller
public class FrozenboxController extends AbstractController{
	
	private static Logger logger = Logger.getLogger(FrozenboxController.class);

	private IFrozenboxService frozenboxService;
	

	public void setFrozenboxService(IFrozenboxService frozenboxService) {
		this.frozenboxService = frozenboxService;
	}
	
	/**
	 * 根据冻存盒ID获取冻存盒基本信息
	 * @param id 冻存盒ID
	 * @return 冻存盒信息
	 */
	@RequestMapping(method=RequestMethod.GET, value="/frozenbox/{id}")
	@ResponseBody
	public String getFrozenboxInfo(@PathVariable String id,
			@RequestParam("callback") String callback,
			@RequestHeader("Accept")String accept) {
		
		FrozenboxList frozenboxs = new FrozenboxList();
		
		
		if( id == null ){
			frozenboxs.setReturnCode(2);
		} else {
			try {
				String[] ids = id.split(",");
				List<Frozenbox> list = frozenboxService.getFrozenboxInfo(Arrays.asList(ids));
				frozenboxs.setFrozenbox(list);
			} catch (Exception e) {
				logger.error(e.getMessage(), e.getCause());
				frozenboxs.setReturnCode(1);
			}
		}
		String result = this.pojo2String(frozenboxs, accept);
		result = callback+result;
		return result;
	}
	
	/**
	 * 根据冻存盒ID列出样本信息
	 * @param id 冻存盒ID
	 * @return 样本信息列表
	 */
	@RequestMapping(method=RequestMethod.GET, value="/frozenbox/{id}/samples")
	@ResponseBody
	public String getFrozenboxSamples(@PathVariable String id,
			@RequestParam("callback") String callback,
			@RequestHeader("Accept")String accept) {
		
		SamplesList samples = new SamplesList();
		

		try {
			List<Samples> list = frozenboxService.getFrozenboxSamples(id);
			samples.setSamples(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e.getCause());
			samples.setReturnCode(1);
		}
		String result = this.pojo2String(samples, accept);
		result = callback+result;
		return result;
	}
}
